const app = getApp()
const observer = require("../../../common/lib/observer").observer
let pc = require("../../../util/play_code")
let store = require("cb_store").store
let ble = require("../../../util/ble")
let settings = require("../../../util/settings")



Page(observer({
	props: {
		common_store: app.data.common_store,
		store: store,
	},
	onShow() {
		this.create_random_str()
		pc.init_cp()
	},
	onLoad() {
		settings.get_settings()
	},
	go(item)  {
		app.go(item)
	},
	// 设置播放设备
	set_play_dev() {
		this.stop()
		this.props.store.play_on_phone = !this.props.store.play_on_phone
	},
	// 生成随机字符串
	create_random_str() {
		this.stop()
		this.props.store.correct_num = 0
		this.props.store.correct_rage = "0.00"
		let r_str = app.random_str(this.props.store.str_total_num)
		this.props.store.r_str = r_str
		this.props.store.w_list = []
		this.props.store.tj_list = []
		for(let i = 0; i < r_str.length; i ++) {
			this.props.store.w_list.push({"word": r_str[i], "input": "*", "color": this.props.store.color.gray})
			this.props.store.tj_list.push(0)
		}
		this.props.store.c_list = app.get_code_list(r_str)
		this.props.store.current_index = 0
		this.props.store.input_index = 0
		this.props.store.w_list[this.props.store.current_index].color = this.props.store.color.green
	},

	// 设置当前的索引
	set_current_index(e) {
		this.stop()
		this.check_input_state(this.props.store.current_index)
		let index = e.currentTarget.dataset.index
		this.props.store.current_index = index
		this.props.store.input_index = index
		this.props.store.w_list[index].color = this.props.store.color.green
	},
	// 发现设备
	discovery_cw() {
		if(this.props.common_store.bt_dev.c_dev.connected) {
			// 断开连接
			ble.close_dev()
		}
		else {
			app.show_n({"msg": "准备连接设备"})
			// 连接设备
			ble.ble_start()
		}
	},

	// 开始播放
	play_or_stop() {
		if(this.props.store.flag_play) {
			this.stop()
		}
		else {
			this.play()
		}
	},

	stop() {
		this.props.store.flag_play = false
		pc.cut()
		// pc.stop_ble_play_code()
		this.props.common_store.play_done_callback_fn = ""
		pc.stop_ble_play_code();
	},

	play() {
		if(!this.props.store.play_on_phone) {
			if(!this.props.common_store.bt_dev.c_dev.connected) {
				app.show_n({"msg": "还没有连接练习器，请先连接练习器后再使用练习器播放电码"})
			}
			else {
				this.props.store.flag_play = true
				this.props.common_store.play_done_callback_fn = this.play_next
				pc.ble_play_code_list(this.props.store.c_list.slice(this.props.store.current_index, this.props.store.c_list.length))
			}
		}
		else {
			this.props.store.flag_play = true
			pc.play_code_list(this.props.store.c_list.slice(this.props.store.current_index, this.props.store.c_list.length), this.play_next)
		}
	},
	play_next() {
		this.props.store.current_index ++ 
		this.check_input_state(this.props.store.current_index - 1)
		if(this.props.store.current_index >= this.props.store.c_list.length) {
			this.props.store.current_index = 0
			this.props.common_store.play_done_callback_fn = ""
			this.props.store.flag_play = false
		}
		else {
			this.props.store.w_list[this.props.store.current_index].color = this.props.store.color.green
			// pc.ble_play_code(this.props.store.c_list[this.props.store.current_index])
		}
		
	},

	// 检查当前输入是否正确
	check_input_state(index) {
		// 如果输入正确
		if(this.props.store.w_list[index].input == "" || this.props.store.w_list[index].input == "*") {
			this.props.store.w_list[index].color = this.props.store.color.gray
		}
		else {
			if(this.props.store.w_list[index].word == this.props.store.w_list[index].input) {
				this.props.store.w_list[index].color = this.props.store.color.blue
				this.props.store.tj_list[index] = 1
				this.calc_summary()
			}
			else {
				this.props.store.w_list[index].color = this.props.store.color.red
				this.props.store.tj_list[index] = 0
				this.calc_summary()
			}
		}
	},
	// 计算准确率
	calc_summary() {
		
		this.props.store.correct_num = this.props.store.tj_list.reduce(function(prev,cur){
			return prev + cur;
		},0);
		this.props.store.correct_rate = String((this.props.store.correct_num / this.props.store.str_total_num * 100).toFixed(2))
	},
	
	press_key(e) {
		let w = e.currentTarget.dataset.word
		this.props.store.w_list[this.props.store.input_index].input = w
		this.check_input_state(this.props.store.input_index)
		if(this.props.store.input_index < this.props.store.w_list.length - 1) {
			this.props.store.input_index ++
		}
	},
	// 设置播放速度
	set_speed(e) {
		// console.log(e)
		let speed = e.detail.value
		settings.set_item("play_u_time", speed)
		
	}
}))